package com.roc.template.cloud.factory.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.roc.template.cloud.CloudConfig;
import com.roc.template.cloud.factory.AccessToken;
import com.roc.template.cloud.util.AccessTokenBean;
import com.roc.template.cloud.util.BasetTokenUtils;
import com.roc.template.cloud.util.HttpClientUtils;
import com.roc.template.exception.BussinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


/**
 * @ClassName AppAccessToken
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/7/29 13 42
 * @Version V1.0
 **/
public class AppAccessToken implements AccessToken {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    public String accessToken = null;
    public String refreshToken = null;
    public long expiredUntil = 0L;

    private CloudConfig cloudConfig;

    public AppAccessToken(CloudConfig cloudConfig) {
        this.cloudConfig = cloudConfig;
    }


    @Override
    public void getAccesssToken() {
        JSONObject param = new JSONObject();
        param.put("appId", cloudConfig.appId);
        param.put("secret",cloudConfig.appSecret);
        param.put("timestamp", System.currentTimeMillis());
        param.put("scope","app");

        String url = cloudConfig.outHost+"/gateway/oauth2/token/getAccessToken";
        String retString = HttpClientUtils.post(url, param.toJSONString());

        JSONObject jsonObject = JSON.parseObject(retString);

        logger.info("获取到accessToken:\n" + jsonObject);
        AccessTokenBean accessTokenBean = BasetTokenUtils.getParam(jsonObject);
        accessToken = accessTokenBean.getAccessToken();
        refreshToken = accessTokenBean.getRefreshToken();
        expiredUntil = accessTokenBean.getExpiredUntil();
    }

    @Override
    public void refreshAccessToken() {
        JSONObject param = new JSONObject();
        param.put("appId", cloudConfig.appId);
        param.put("refreshToken", refreshToken);
        param.put("timestamp", System.currentTimeMillis());
        param.put("scope", "app");

        String url = cloudConfig.outHost+"/gateway/oauth2/token/refreshToken";
        String retString = HttpClientUtils.post(url, param.toJSONString());

        JSONObject jsonObject = JSON.parseObject(retString);
        logger.info("刷新accessToken:\n" + jsonObject);
        AccessTokenBean accessTokenBean = BasetTokenUtils.getParam(jsonObject);
        accessToken = accessTokenBean.getAccessToken();
        refreshToken = accessTokenBean.getRefreshToken();
        expiredUntil = accessTokenBean.getExpiredUntil();
    }

    @Override
    public void checkAndRefreshAccessToken() {
        if (expiredUntil == 0L) {
            getAccesssToken();
        } else if (expiredUntil< 60L) {
            refreshAccessToken();
        }
    }

    public JSONObject acquirecontext(Map<String, String> param) throws Exception {
        checkAndRefreshAccessToken();
        param.put("appid", cloudConfig.appId);
        String url = cloudConfig.outHost+"/gateway/ticket/user/acquirecontext?accessToken=";
        url += accessToken;
        String retString = HttpClientUtils.post(url, JSON.toJSONString(param));
        JSONObject jsonObject = JSON.parseObject(retString);
        logger.info("解析ticket包含的人员信息:\n" + retString);
        if (jsonObject.getBoolean("success")) {
            return jsonObject.getJSONObject("data");
        } else {
            throw new BussinessException(jsonObject.getString("error"));
        }
    }

}
